import SocketUser_1 from './SocketUser.js'
import helper from './helper.js'

var Message = (function() {

	/** 手机震动 */
	function vibrateShort() {

		uni.vibrateShort({
			success: function() {}
		});

	}

	/** 向会话列表追加会话消息 */
	function PushMessage(v, msg, isOffline, isSend) {
		
		console.log(msg.title)

		for (var i = 0; i < v.messages.length; i++) {
			var item = v.messages[i];
			if (item.key == msg.key) {
				v.messages.splice(i, 1);
				msg.count = msg.count + item.count;
			}
		}

		var chatmsg = helper.SesstionMsgForm(msg,isSend);

		switch (msg.type) {
			case 'img':
				msg.message = '[图片]';
				break;
			case 'voice':
				msg.message = '[语音]';
				break;
			case 'map':
				msg.message = '[位置]';
				break;
			case 'card':
				msg.message = '[信息卡片]';
				break;
			case 'product':
				msg.message = '[产品]';
				break;
			case 'order':
				msg.message = '[订单信息]';
				break;
			case 'redEnvelope':
				msg.message = '[红包]';
				break;
			case 'text':
			default:
				break;
		}
		
		console.log(JSON.stringify(msg))

		v.messages.splice(0, 0, msg);

		if (!isOffline) {

			if (v.sesstion == msg.key.toString()) {
				msg.count = 0;
				v.setMsg(chatmsg);
			} else if (v.isHide) {
				plus.push.createMessage(msg.title + ':' + msg.message);
			} else {
				vibrateShort();
			}

		}

		helper.SetChatMsg(chatmsg);
		helper.SetSesstion(v.messages);

	}

	function ItemMsessageForm(v, date) {
		var reg = new RegExp("'", "g")
		
		
		console.log(date.userParameter)

		var userinfo = JSON.parse(date.userParameter.replace(reg, '"'));
		var parameter = JSON.parse(date.messageParameter.replace(reg, '"'));

		PushMessage(v, {
			title: userinfo.userName,
			url: [userinfo.headImg],
			message: date.message,
			time: helper.timeForm(date.sendDateTime),
			count: 1,
			stick: false,
			key: date.userKey,
			type: parameter.type,
			len: parameter.length,
			disabled: false
		});
	}

	function Message(v, type, date) {
		switch (type) {

			case 'clients':
				console.log('clients:' + date.length)
				break;
			case 'length':
			
			var datatime = new Date();
				v.heartbeattime = datatime.getHours() +':'+datatime.getMinutes()+':'+datatime.getSeconds();
				// console.log('length:' + date)
				break;
			case 'userlogin':
				console.log('userlogin:' + date)
				break;
			case 'offline':
				console.log('offline:' + date)
				date = JSON.parse('[' + date + ']');
				for (var i = 0; i < date.length; i++) {
					ItemMsessageForm(v, date[i], true);
				}
				break;
			case 'current':
				console.log('current:' + date)
				ItemMsessageForm(v, date);
				break;

			default:

				break;

		}
	}

	return Message;

}());


export default {
	Task: Message,
	Send: function (v, task) {
		
		var msg = {
			title: task.taskUserInfo.userName,
			url: [task.taskUserInfo.headImg],
			message: task.msg.content.text,
			time: task.msg.time,
			count: 0,
			stick: false,
			key: v.sesstion,
			type: task.msg.type,
			len: task.msg.content.length,
			disabled: false
		};
		
		for (var i = 0; i < v.messages.length; i++) {
			var item = v.messages[i];
			if (item.key == msg.key) {
				v.messages.splice(i, 1);
			}
		}
		
		var temp = JSON.parse(JSON.stringify(msg));
		temp.title = task.msg.userinfo.username;
		temp.url = [task.msg.userinfo.face];
		var chatmsg = helper.SesstionMsgForm(temp,true);
		helper.SetChatMsg(chatmsg,v.sesstion);
		
		switch (msg.type) {
			case 'img':
				msg.message = '[图片]';
				break;
			case 'voice':
				msg.message = '[语音]';
				break;
			case 'map':
				msg.message = '[位置]';
				break;
			case 'card':
				msg.message = '[信息卡片]';
				break;
			case 'product':
				msg.message = '[产品]';
				break;
			case 'order':
				msg.message = '[订单信息]';
				break;
			case 'redEnvelope':
				msg.message = '[红包]';
				break;
			case 'text':
			default:
				break;
		}
		
		v.messages.splice(0, 0, msg);
		helper.SetSesstion(v.messages);
		
		
	}

};
